package cn.loioi.distribution;

import org.springframework.beans.factory.InitializingBean;

import cn.loioi.distribution.cfg.DistributionCfgManage;
import cn.loioi.distribution.db.DbManger;
import cn.loioi.distribution.level.DistributionLevelManage;
import cn.loioi.distribution.member.DistributionMemberManage;

public class DistributionManage implements InitializingBean {
	private DbManger dbManger;
	private DistributionCfgManage distributionCfgManage;
	private DistributionLevelManage distributionLevelManage;
	private DistributionMemberManage distributionMemberManage;

	public DistributionCfgManage getDistributionCfgManage() {
		return distributionCfgManage;
	}

	public DistributionLevelManage getDistributionLevelManage() {
		return distributionLevelManage;
	}

	public DistributionMemberManage getDistributionMemberManage() {
		return distributionMemberManage;
	}

	private void setDistributionCfgManage(DistributionCfgManage distributionCfgManage) throws Exception {
		this.distributionCfgManage = distributionCfgManage;
		this.distributionCfgManage.setDbManger(dbManger);

		this.distributionCfgManage.afterPropertiesSet();
	}

	private void setDistributionLevelManage(DistributionLevelManage distributionLevelManage) throws Exception {
		this.distributionLevelManage = distributionLevelManage;
		this.distributionLevelManage.setDbManger(dbManger);

		this.distributionLevelManage.afterPropertiesSet();
	}

	private void setDistributionMemberManage(DistributionMemberManage distributionMemberManage) throws Exception {
		this.distributionMemberManage = distributionMemberManage;
		this.distributionMemberManage.setDbManger(dbManger);

		this.distributionMemberManage.afterPropertiesSet();
	}

	public void setDbManger(DbManger dbManger) {
		this.dbManger = dbManger;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		if (dbManger == null) throw new IllegalArgumentException("DbManger is null");

		this.setDistributionCfgManage(new DistributionCfgManage());
		this.setDistributionLevelManage(new DistributionLevelManage());
		this.setDistributionMemberManage(new DistributionMemberManage());
	}
}
